package function

import (
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

type DefaultFloatFunction struct {
	size int
	data []interface{}
}

func DefaultFloat(size int) *DefaultFloatFunction {
	return &DefaultFloatFunction{
		size: size,
		data: make([]interface{}, 0, size),
	}
}

func (d *DefaultFloatFunction) Build() []interface{} {
	rand.Seed(time.Now().Unix())
	for i := 0; i < d.size; i++ {
		d.data = append(d.data, rand.Float32()*100.0)
	}
	return d.data
}

type SpecifyFloatFunction struct {
	size  int
	right string
	data  []interface{}
}

func SpecifyFloat(size int, right string) *SpecifyFloatFunction {
	return &SpecifyFloatFunction{
		size:  size,
		right: "%" + right + "f",
		data:  make([]interface{}, 0, size),
	}
}

func (s *SpecifyFloatFunction) Build() []interface{} {
	rand.Seed(time.Now().Unix())
	for i := 0; i < s.size; i++ {
		value, _ := strconv.ParseFloat(fmt.Sprintf(s.right, rand.Float64()*100), 64)
		s.data = append(s.data, value)
	}
	return s.data
}

type RangeFloatFunction struct {
	size   int
	min    float64
	max    float64
	format string
	data   []interface{}
}

func RangeFloat(size int, min, max float64, format string) *RangeFloatFunction {
	return &RangeFloatFunction{
		size:   size,
		min:    min,
		max:    max,
		format: "%" + format + "f",
		data:   make([]interface{}, 0, size),
	}
}

func (r *RangeFloatFunction) Build() []interface{} {
	rand.Seed(time.Now().Unix())
	for i := 0; i < r.size; i++ {
		temp := rand.Float64() * (r.max - r.min)
		value, _ := strconv.ParseFloat(fmt.Sprintf(r.format, temp), 64)
		r.data = append(r.data, value)
	}
	return r.data
}
